package com.jiang.mall.mallproduct.product.app;

import com.jiang.common.utils.R;
import com.jiang.mall.mallproduct.product.entity.CategoryEntity;
import com.jiang.mall.mallproduct.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品三级分类
 *
 * @author zhangsan
 * @email 2469939264@qq.com
 * @date 2021-03-22 01:05:29
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 找到所有的分类
        List<CategoryEntity> allMenus = categoryService.getData();
        // 找到所有的一级分类
        List firstMenu = allMenus.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity, allMenus));
                    return categoryEntity;
                })
                .sorted((categoryEntity1, categoryEntity2) -> (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) - (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort()))
                .collect(Collectors.toList());
        return R.ok().put("data", firstMenu);
    }

    //递归查找所有菜单的子菜单
    public List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> allMenus) {
        List treeList = allMenus.stream()
                .filter(categoryEntity -> root.getCatId() == categoryEntity.getParentCid())
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity, allMenus));
                    return categoryEntity;
                })
                .sorted((categoryEntity1, categoryEntity2) -> (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) - (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort()))
                .collect(Collectors.toList());
        return treeList;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity categoryEntity) {
        categoryService.updateCascade(categoryEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));
        // 逻辑删除
        categoryService.removeMenusByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
